﻿@using KelonProud.Mvc.Models

@{
    Layout = "~/Views/Shared/_EditMaster.cshtml";
}

@section styles
    {
    <style>
        .content {
            padding: 20px;
        }

        .panel-tool {
            line-height: 32px;
        }

        .panel-toolbar {
            float: right;
        }

        .panel-body {
            overflow: auto;
            height: calc(100% - 40px);
        }

        .panel {
            margin-bottom: 0px;
        }

        #splitterContent {
            height: 100%;
        }

        .fixed-table-toolbar {
            padding-left: 20px;
        }
    </style>
}
<link href="~/lib/jquery-splitter/jquery.splitter.css" rel="stylesheet" />
<div id="splitterContent">
    <div id="leftPanel">
        <div class="panel panel-default   full-height">
            <div class="panel-heading">
                数据表分类
                <div class="panel-toolbar">
                    <button class="btn btn-xs btn-success" data-toggle="tooltip" data-placement="bottom" onclick="onAddTableCategory()" title="添加下级分类" style="margin-top: -2px;"><i class="fa fa-plus"></i></button>
                    <button class="btn btn-xs btn-danger" data-toggle="tooltip" data-placement="bottom" onclick="onDeleteTableCategory()" title="删除当前分类" style="margin-top: -2px;"><i class="fa fa-remove"></i></button>
                    <button class="btn btn-xs btn-primary" data-toggle="tooltip" data-placement="bottom" onclick="loadTree()" title="刷新树结构" style="margin-top: -2px;"><i class="fa fa-refresh"></i></button>
                </div>
            </div>
            <div class="panel-body">
                <ul id="typeTree" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div id="rightPanel">

        <div class="panel panel-default   full-height">
            <div class="panel-heading" id="oprateName">
                操作说明
            </div>
            <div class="panel-body modal-box hidden" id="detailDiv">
                <form class="form-horizontal" id="EditForm" name="EditForm" validate method="post" role="form">
                    @Html.Hidden("Id", 0)
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">上级</label>
                            <div class="col-sm-10">
                                @Html.Partial("~/Views/Shared/Modals/Components/_DropDownTreeView.cshtml", new DropDownTreeModel
                                {
                                    Url = "/api/services/table/tableCategory/GetCategoryTreeJson?isAll=false",
                                    IsMilti = false,
                                    Name = "ParentId",
                                })
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">名称</label>
                            <div class="col-sm-10">
                                @Html.TextBox("Name", "", new { @class = "form-control validate" })
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">编码</label>

                            <div class="col-sm-10">
                                @Html.TextBox("Code", "", new { @class = "form-control validate" })
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">全名</label>
                            <div class="col-sm-10">
                                @Html.TextBox("FullName", "", new { @class = "form-control validate", @disabled = "" })
                            </div>
                        </div>
                    </div>
                    @Html.Partial("~/Views/Shared/Modals/_ModalFooterWithSaveAndCancel.cshtml")
                </form>
            </div>
            <div class="panel-body " id="remarkDiv">
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script src="~/lib/jquery-splitter/jquery.splitter-0.14.0.js"></script>
    <script>
        var fullName = "";
        var isInserting = false;
        var currentSelectNodeId = "";

        $(function () {
            $('#splitterContent').split({ orientation: 'vertical', position: '300px', limit: 200 });
            loadTree();

            $("#Name").keyup(function () {
                var newFullName = (fullName === "" || fullName == null)
                    ? $("#Name").val()
                    : fullName + ">" + $("#Name").val();

                $("#FullName").val(newFullName);

                onUpdateNodeName($("#Name").val());
            });
        });

        function loadTree() {
            var setting2 = {
                view: {
                    showLine: true,
                    selectedMulti: true
                },
                data: {
                    key: {
                    },
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: zTreeOnClick
                }
            }

            abp.ui.setBusy("#leftPanel");
            abp.ajax({
                url: '/api/services/table/tableCategory/GetCategoryTreeJson?isAll=true'
            }).done(function (data) {
                $.fn.zTree.init($("#typeTree"), setting2, data);
                abp.ui.clearBusy("#leftPanel");
                $("#detailDiv").addClass("hidden");
                $("#remarkDiv").removeClass("hidden");
                onSetDefualtRemark();
            });
        }

        function zTreeOnClick(event, treeId, treeNode) {
            if (isInserting && currentSelectNodeId !== treeNode.id) {
                abp.message.confirm("您有新增大类未保存，该操作将取消保存数据！",
                    "确认取消新增",
                    function (isConfirm) {
                        if (isConfirm) {
                            resetTree();
                        } else {
                            var treeObj = $.fn.zTree.getZTreeObj("typeTree");
                            var currentSelectNode = treeObj.getNodesByParam("id", currentSelectNodeId, null)[0];
                            treeObj.selectNode(currentSelectNode);
                        }
                    });
            }

            if (!isInserting) {
                if (treeNode.id !== "-1") {
                    $("#detailDiv").removeClass("hidden");
                    $("#remarkDiv").addClass("hidden");
                } else {
                    $("#detailDiv").addClass("hidden");
                    $("#remarkDiv").removeClass("hidden");
                    onSetDefualtRemark();
                }
                onLoadCategoryData(treeNode);
            }
        }

        function onLoadCategoryData(treeNode) {
            onLoadonLoadCategoryParent(treeNode.pId);

            $("#Id").val(treeNode.id);
            $("#Name").val(treeNode.name);
            $("#Code").val(treeNode.code);
            $("#FullName").val(treeNode.fullName);
            currentSelectNodeId = treeNode.id;
        }

        function onLoadonLoadCategoryParent(id) {
            var treeObj = $.fn.zTree.getZTreeObj("zDropDownTree_ParentId");
            var node = treeObj.getNodeByParam("id", id, null);

            if (node != null) {
                treeObj.selectNode(node);
                $("#text_ParentId").val(node.name);
                $("#ParentId").val(node.id);
                fullName = node.fullName;
                onSetEditRemark();
            } else {
                fullName = "";
                onSetDefualtRemark();
            }
        }

        function onAddTableCategory() {
            var treeObj = $.fn.zTree.getZTreeObj("typeTree");
            var nodes = treeObj.getSelectedNodes();

            if (nodes.length === 0) {
                abp.message.warn("请先选择一个分类节点，再进行添加", "提示");
            } else {
                $("#detailDiv").removeClass("hidden");
                $("#remarkDiv").addClass("hidden");
                $("#Name").val("");
                $("#Code").val("");
                $("#Id").val("0");

                onLoadonLoadCategoryParent(nodes[0].id);

                if (isInserting) {
                    var newNode = treeObj.getNodesByParam("id", "-3", null);

                    treeObj.selectNode(newNode[0]);
                } else {
                    var newNodes = [
                        { id: -3, pId: nodes[0].id, name: "新的分类" }
                    ];

                    newNodes = treeObj.addNodes(nodes[0], newNodes);
                    treeObj.selectNode(newNodes[0]);
                    isInserting = true;
                    currentSelectNodeId = newNodes[0].id;

                    onSetInsertRemark();
                }
            }
        }

        function onDeleteTableCategory() {
            var treeObj = $.fn.zTree.getZTreeObj("typeTree");
            var nodes = treeObj.getSelectedNodes();

            if (nodes.length === 0) {
                abp.message.warn("请先选择一个分类节点，再进行删除操作", "提示");
            } else {
                abp.message.confirm("您确认删除该分类吗？",
                    "删除分类",
                    function (isConfirm) {
                        if (isConfirm) {
                            var $service = abp.services.table.tableCategory;
                            $service.delete({ id: nodes[0].id }).done(function () {
                                abp.notify.success("分类删除成功！");
                                var treeObj = $.fn.zTree.getZTreeObj("typeTree");
                                var currentSelectNode = treeObj.getNodesByParam("id", nodes[0].id, null)[0];
                                treeObj.removeNode(currentSelectNode);
                                treeObj.refresh();
                                onSetDefualtRemark();
                            });
                        }
                    });
            }
        }

        //重置新增
        function resetTree() {
            if (isInserting) {
                var treeObj = $.fn.zTree.getZTreeObj("typeTree");
                var currentSelectNode = treeObj.getNodesByParam("id", currentSelectNodeId, null)[0];
                treeObj.removeNode(currentSelectNode);
                treeObj.refresh();
                isInserting = false;
                $("#detailDiv").addClass("hidden");
                $("#remarkDiv").removeClass("hidden");
            }
            onSetDefualtRemark();
        }

        function onUpdateNodeId(id) {
            var treeObj = $.fn.zTree.getZTreeObj("typeTree");
            var currentSelectNode = treeObj.getNodesByParam("id", currentSelectNodeId, null)[0];

            currentSelectNode.id = id;
            treeObj.updateNode(currentSelectNode);
        }

        function onUpdateNodeName(name) {
            var treeObj = $.fn.zTree.getZTreeObj("typeTree");
            var currentSelectNode = treeObj.getNodesByParam("id", currentSelectNodeId, null)[0];

            currentSelectNode.name = name;
            treeObj.updateNode(currentSelectNode);
        }

        function saveFormEvent(_$form) {
            var _navService = abp.services.table.tableCategory;
            var obj = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js
            obj.roleIds = [];
            obj.editionIds = [];

            abp.ui.setBusy(_$form);
            _navService.save(obj).done(function (d) {
                if (obj.Id === "0") {
                    onSetEditRemark();
                    abp.notify.success("分类创建成功！");
                    $("#Id").val(d.id);
                    onUpdateNodeId(d.id);
                } else {
                    abp.notify.success("分类修改成功！");
                }

                isInserting = false;

            }).always(function () {
                abp.ui.clearBusy(_$form);
            });
        }

        function closeFormEvent() {
            resetTree();
        }

        function onSetInsertRemark() {
            $("#oprateName").text("新增分类");
            $("button[type='reset']").show();
        }

        function onSetEditRemark() {
            $("button[type='reset']").hide();
            $("#oprateName").text("编辑分类");
        }

        function onSetDefualtRemark() {
            $("button[type='reset']").hide();
            $("#oprateName").text("操作说明");
        }
    </script>
    <div>

    </div>
}